package main

import (
	"fmt"
	"net"
	"time"
)

// 模拟客户端
func main() {
	fmt.Println("Client Start")
	time.Sleep(1 * time.Second)
	// 1 直接链接远程服务器 得到一个conn链接
	conn, err := net.Dial("tcp", "localhost:8999")

	if err != nil {
		fmt.Println("client start err,exit!")
		return
	}
	count := 0
	for {
		// 2 链接调用Write 写数据
		count++
		_, err := conn.Write([]byte(fmt.Sprintf("第%d次 hello world V0.0.1", count)))
		if err != nil {
			fmt.Println("write conn err ", err)
			return
		}

		buf := make([]byte, 512)
		cnt, err := conn.Read(buf)
		if err != nil {
			fmt.Println("read buff err ", err)
			return
		}
		fmt.Printf("server callback：%s，cnt=%d\n", buf, cnt)
		// cpu 阻塞
		time.Sleep(1 * time.Second)
	}
}
